﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameCtr : MonoBehaviour {
    public GameObject gm;
    public GameObject cvs;
    public GameObject bg;
    public Text gameOverTextPrefab;

    public GameObject scoreTextPrefab;
    public GameObject scorePrefab;

    Text gameOverText;

    public struct Ball
    {
        public int tag;
        public float radius;
        public GameObject ball;
        public float timer;
        public bool isActivated;
        public int direction;
        public float speed;
    };

    public static class GameData
    {
        public static int score = 0;
        public static float maxDistance = .0f;
        public static GameObject scoreGo;
        public static int multiple = 1;
    };

    public List<Ball> ballsList = new List<Ball>();

    GameObject mainBall;
    Vector3 mainBallOldPos;

    public static bool isPause;
    public static bool isGameOver;
    public static bool isRotateBonus;

    float gameOverTextTimer = .0f;
    int flag = 1;
    float needTime = 0.7f;

    public static float bgShakeMultiple = 20.0f;

    // Use this for initialization
    void Awake () {
        isPause = false;
        isGameOver = false;
        isRotateBonus = false;
        Time.timeScale = 1;

    Screen.SetResolution(1920, 1080, false);

        mainBall = gm.GetComponent<MainBallCtr>().mainBall;

        mainBallOldPos = mainBall.GetComponent<RectTransform>().anchoredPosition3D;

        GameData.scoreGo = Instantiate(scorePrefab, cvs.transform);
	}
	
	// Update is called once per frame
	void Update () {
        if (isPause == false)
        {
            int index = IsCollision();
            
            if(index == -1)
            {
                Destroy(mainBall);
                isPause = true;
                isGameOver = true;
            }
            else if (index != -2)
            {
                CollisionDestroy(index);
            }
        }
        else if(isGameOver)
        {
            Time.timeScale = 0;
            if (gameOverText == null)
            {
                gameOverText = Instantiate(gameOverTextPrefab);
                gameOverText.transform.SetParent(cvs.transform);
                gameOverText.GetComponent<RectTransform>().anchoredPosition3D = Vector3.zero;
            }

            ShowGameOverText();
        }

        //Pause test
        if (Input.GetKeyDown(KeyCode.P) && !isGameOver)
        {
            if (Time.timeScale == 1)
            {
                Time.timeScale = 0;
                isPause = true;
            }
            else
            {
                Time.timeScale = 1;
                isPause = false;
            }
        }

        if(isGameOver && Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(0);
        }
	}

    int IsCollision()
    {
        mainBallOldPos = mainBall.GetComponent<RectTransform>().anchoredPosition3D;

        foreach(Ball gb in ballsList)
        {
            if (gb.tag != BallsCtr.BallTag.centerBallTag && gb.isActivated)
            {
                if (Vector3.Distance(mainBallOldPos, gb.ball.GetComponent<RectTransform>().anchoredPosition3D) <= MainBallCtr.mainBallRadius + gb.radius)
                {
                    StartCoroutine(gm.GetComponent<BgCtr>().OnShakeBG());

                    Destroy(GameData.scoreGo);
                    GameData.scoreGo = Instantiate(scorePrefab, cvs.transform);
                    GameData.scoreGo.GetComponent<ScoreText>().Init(0);

                    if (gb.tag == BallsCtr.BallTag.startBallTag || gb.tag == BallsCtr.BallTag.ballTag)
                    {
                        int multiple = GameData.multiple;
                        GameObject go = Instantiate(scoreTextPrefab, bg.transform);                        
                        go.GetComponent<RectTransform>().anchoredPosition3D = gb.ball.GetComponent<RectTransform>().anchoredPosition3D;
                        go.GetComponent<ScoreText>().Init(1, 1 * multiple);
                        GameData.score += 1 * multiple;
                 
                    }
                    /*
                    else if(gb.tag == 2)
                    {
                        GameObject go = Instantiate(scoreTextPrefab, bg.transform);                      
                        go.GetComponent<RectTransform>().anchoredPosition3D = gb.ball.GetComponent<RectTransform>().anchoredPosition3D;
                        go.GetComponent<ScoreText>().Init(1, 2);
                        GameData.score += 2;
                    }
                    */

                    if (gm.GetComponent<MainBallCtr>().mainBallSpeed > 0)
                    {
                        if(gb.tag == BallsCtr.BallTag.specialBallTag)
                        {
                            return -1;
                        }
                        //gm.GetComponent<MainBallCtr>().mainBallSpeed += 15 * gm.GetComponent<MainBallCtr>().accelerateValue;
                        gm.GetComponent<MainBallCtr>().mainBallSpeed = -gm.GetComponent<MainBallCtr>().mainBallSpeed;
                    }
                    else
                    {
                        gm.GetComponent<MainBallCtr>().mainBallSpeed -= 20.0f * gm.GetComponent<MainBallCtr>().accelerateValue;
                    }

                    if (gb.tag == BallsCtr.BallTag.rotateBonusTag)
                    {
                        // RotateBonusBall
                        isRotateBonus = true;
                    }

                    GameData.scoreGo.GetComponent<Text>().text = GameData.score + "";

                    return ballsList.IndexOf(gb);
                }
            }
        }

        return -2;
    } 

    void CollisionDestroy(int index)
    {
        Destroy(ballsList[index].ball);
        ballsList.RemoveAt(index);
    }

    void ShowGameOverText()
    {
        // 游戏结束 渐变

        gameOverTextTimer += Time.fixedDeltaTime * flag;
        if (gameOverTextTimer >= needTime)
        {
            gameOverTextTimer = needTime;
            flag = -1;
        }
        else if (gameOverTextTimer <= .0f)
        {
            gameOverTextTimer = .0f;
            flag = 1;
        }
        gameOverText.color = new Color(255, 0, 0, 1 * (gameOverTextTimer + 0.2f) / needTime);
    }


}
